class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # For admins, they can manage some contests.
  has_many :managements, dependent: :destroy
  has_many :supervisees, through: :managements, class_name: 'Contest'

  # For students, they can enroll some contests.
  has_many :enrollments, dependent: :destroy
  has_many :watching_contests, through: :enrollments, class_name: 'Contest'
  
  # For teachers, they can judge some products.
  has_many :results, dependent: :destroy
  has_many :judging_products, through: :results, class_name: 'Product'

  has_many :profiles, dependent: :destroy
  has_many :joined_products, through: :profiles, class_name: 'Product'

  def is?(a_role)
    YAML::load(self.roles).any? { |role| role == a_role }
  end
end
